package com.ym.b2b.order.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ym.b2b.commons.conf.WebTokenUser;
import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.order.service.EmSOrderConfirmItemService;
import com.ym.b2b.order.sqlbean.EmSOrderConfirmItem;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

@Controller
@RequestMapping("/trade/item")
public class EmSOrderConfirmItemController extends BaseController {

    @Autowired
    private EmSOrderConfirmItemService emSOrderConfirmItemService;

    @RequestMapping(value = "deleteEmSOrderConfirmItem.s")
    @ResponseBody
    public Result deleteEmSOrderConfirmItem(EmSOrderConfirmItem emSOrderConfirmItem, HttpServletRequest req) {

        Result ret = new Result();
        WebTokenUser tokenUser = getWebTokenUser(req);
        if (tokenUser != null) {
            emSOrderConfirmItem.setUser(tokenUser);
        }
        try {
            ret = emSOrderConfirmItemService.deleteEmSOrderConfirmItem(emSOrderConfirmItem,req);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;

    }

    /**
     * 可退货申请列表
     * 
     * @Title: searchPageOrderItemReturn
     * @Throws
     */

    @RequestMapping("searchPageOrderItemReturn.s")
    @ResponseBody
    public Result searchPageOrderItemReturn(EmSOrderConfirmItem emSOrderConfirmItem, Page<EmSOrderConfirmItem> page,
                                            HttpServletRequest req) {
        Result result = new Result();
        try {
            WebTokenUser tokenUser = getWebTokenUser(req);
            if (tokenUser != null) {
                emSOrderConfirmItem.setBuyerCode(tokenUser.getBuyerCode());
            }
            page = emSOrderConfirmItemService.searchPageEmSOrderConfirmItemReturn(emSOrderConfirmItem, page);
            result.setResultPage(page);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * 申请退货操作
     * 
     * @Title: updateConfirmItemForIsReturn
     * @Throws
     */
    @RequestMapping("updateConfirmItemForIsReturn.s")
    @ResponseBody
    public Result updateConfirmItemForIsReturn(EmSOrderConfirmItem emSOrderConfirmItem, HttpServletRequest req) {
        Result result = new Result();
        try {
            WebTokenUser token = getWebTokenUser(req);
            if (token == null) {
                result.setCodeResult(Result.CODE_100, "请先登陆采购商!");
                return result;
            }
            result = emSOrderConfirmItemService.updateConfirmItemForIsReturn(emSOrderConfirmItem);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * 批量申请退货操作
     * 
     * @Title: updateMultipleConfirmItemForIsReturns
     * @Throws
     */
    @RequestMapping("updateMultipleConfirmItemForIsReturns.s")
    @ResponseBody
    public Result updateMultipleConfirmItemForIsReturns(EmSOrderConfirmItem emSOrderConfirmItem, HttpServletRequest req) {
        Result result = new Result();
        try {
            WebTokenUser token = getWebTokenUser(req);
            if (token == null) {
                result.setCodeResult(Result.CODE_100, "请先登陆采购商!");
                return result;
            }
            result = emSOrderConfirmItemService.updateMultipleConfirmItemForIsReturns(emSOrderConfirmItem);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }
}
